package baiqi.sishen.community.controller;

import baiqi.sishen.community.enums.NotificationTypeEnum;
import baiqi.sishen.community.execption.CustomizeErrorCode;
import baiqi.sishen.community.execption.CustomizeException;
import baiqi.sishen.community.model.Notification;
import baiqi.sishen.community.model.User;
import baiqi.sishen.community.service.NotificationService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 兕神
 * @version 1.0
 * DateTime: 2023/5/2
 */
@Controller
public class NotificationController {

    @Resource
    private NotificationService notificationService;

    @GetMapping("notification/{id}")
    public String read(
            @PathVariable("id") Long id,
            HttpServletRequest request) {
        //获取用户
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            throw new CustomizeException(CustomizeErrorCode.USER_NOT_FOUND);
        }

        //通知设为已读
        Notification notification = notificationService.read(id, user);
        if (notification.getType().equals(NotificationTypeEnum.REPlY_QUESTION.getType()) ||
                notification.getType().equals(NotificationTypeEnum.REPLY_COMMENT.getType()) ||
                notification.getType().equals(NotificationTypeEnum.THUMB_COMMENT.getType()) ||
                notification.getType().equals(NotificationTypeEnum.COLLECT_QUESTION.getType())) {
            return "redirect:/question/" + notification.getQuestionId();
        }

        return "redirect:/profile/replies";
    }
}
